﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using IM;

namespace HistoryToMail
{
    public delegate void PluginSettingWhereChanged(String settings);

    public delegate void LogMessage(String text);

    public enum MessageType{inp, outp} ;

    public class HistoryToMailEngine
    {
        private PluginSettings pluginSettings = new PluginSettings();
        private IMMessagesStore messageStore;
        private MailSender sender;
        private LogsForm internalLogger;

        public event PluginSettingWhereChanged SettingsChanged;
        public event LogMessage LogMsgAvailable;



        public HistoryToMailEngine()
        {
            messageStore = new IMMessagesStore(ref pluginSettings);
            messageStore.OnMsgToSend += MessageToSendAvailable;
            sender = new MailSender(ref pluginSettings.smtpSettings);
            sender.LoggerOut += Log;
            changeInternalLoggerState();
        }

        public void Stop()
        {
            messageStore.PushhMessageSendProcessAndStop();
            messageStore = null;
            sender.Stop();
            disableInternalLogger();
        }


        private void changeInternalLoggerState()
        {
            if (pluginSettings == null) return;
            if (pluginSettings.logEnable)
            {
                enableInternalLogger();
            }

            if (!pluginSettings.logEnable)
            {
                disableInternalLogger();
            }
        }

        private void enableInternalLogger()
        {
            if (internalLogger == null)
            {
                internalLogger = new LogsForm();
                internalLogger.Show();
            }
        }

        private void disableInternalLogger()
        {
            if (internalLogger != null)
            {
                internalLogger.Close();
                internalLogger = null;
            }
        }


        

        public void loadSettingsFromString(String Data)
        {
            pluginSettings = PluginSettings.FromString(Data);
            if (sender != null)
            {
                sender.changeSettings(ref pluginSettings.smtpSettings);
            }
            if (messageStore != null)
            {
                messageStore.changeSettings(ref pluginSettings);
            }
            changeInternalLoggerState();
        }

        public void showSettingsDialog()
        {
            pluginSettings.showSettingsDialog();
            if (SettingsChanged != null)
            {
                try
                {
                    SettingsChanged(pluginSettings.ToString());
                }
                catch (Exception)
                {
                }
            }
            //Push settings to components
            changeInternalLoggerState();
        }


        public void addMessage(String protocol, String sender, String receiver,
                               MessageType Type, String body, DateTime time)
        {
            if (messageStore != null)
            {
                messageStore.addMessage(protocol, sender, receiver, Type, body, time);
            }
        }

        private void MessageToSendAvailable(MessageToSend msg)
        {
            if (LogMsgAvailable != null)
            {
                String textToSend = "==============================================\r\n\r\n";
                textToSend += msg.Subject + "\r\n\r\n" + msg.Body + "\r\n";
                LogOut(textToSend);
            }
            if (sender != null)
            {
                sender.AddMessageToSend(msg);
            }
        }

        private void LogOut(String textToSend)
        {
            try
            {
                LogMsgAvailable(textToSend);
            }
            catch (Exception)
            {
                // Nothing to do
            }
        }

        private void Log(String text)
        {
            if (LogMsgAvailable != null)
            {
                try
                {
                    LogMsgAvailable(text);
                }
                catch (Exception)
                {
                }
            }
            try
            {
                AddLog(text);
            }
            catch (Exception)
            {
            }
        }

        public void AddLog(String text)
        {
            if (internalLogger != null)
            {
                internalLogger.AddLog(text);
            }
        }


    }
}
